package com.jmp.api.controller;

import com.jmp.api.dto.MessageDto;
import com.jmp.api.dto.MessagePushDto;
import com.jmp.api.dto.WxSubscribeDto;
import com.jmp.api.service.MessageService;
import com.jmp.api.service.MessageWxService;
import com.jmp.base.MessageApi;
import com.jmp.base.dto.*;
import lombok.extern.slf4j.Slf4j;
import one.stand.model.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 消息
 *
 * @author cjr
 */
@RestController
@Slf4j
public class MessageController implements MessageApi {

    @Autowired
    private MessageService messageService;
    @Autowired
    private MessageWxService messageWxService;

    @Override
    public ResultModel savePush(@RequestBody MessageSavePushRequest request) {
        messageService.savePush(request.getUserId(), request.getCompanyKey(), request.getPushCid());
        return ResultModel.success();
    }

    @Override
    public ResultModel push(@RequestBody MessagePushRequest request) {
        MessagePushDto messageDto = MessagePushDto.builder()
                .pushClient(request.getClientEnum())
                .type(request.getType())
                .fromType(request.getFromType())
                .fromId(request.getFromId())
                .toType(request.getToType())
                .toId(request.getToId())
                .toChildType(request.getToChildType())
                .toIds(request.getToIds())
                .title(request.getTitle())
                .fillData(request.getFillData())
                .pageParams(request.getPageParams())
                .build();
        if (Objects.nonNull(request.getToType()) && MessageApi.TYPE_ALL == request.getToType()) {
            messageService.pushAll(messageDto);
        } else {
            messageService.push(messageDto);
        }

        return ResultModel.success();
    }

    @Override
    public ResultModel wxService(@RequestBody MessageWxServiceRequest request) {
        MessageDto messageDto = MessageDto.builder()
                .noticeEnums(request.getNoticeEnums())
                .accountType(request.getAccountType())
                .toUserId(request.getToUserId())
                .fillData(request.getFillData())
                .parameter(request.getParameter())
                .companyId(request.getCompanyId())
                .formId(request.getFormId())
                .build();
        messageWxService.sendTemplateWx(messageDto);
        return ResultModel.success();
    }

    @Override
    public ResultModel wxSubscribe(@RequestBody MessageWxSubscribeDto request) {
        messageWxService.wxSubscribe(WxSubscribeDto.builder()
                .type(request.getType())
                .toId(request.getToId())
                .pageArr(request.getPageArr())
                .dataArr(request.getDataArr())
                .build()
        );
        return ResultModel.success();
    }

    @Override
    public ResultModel wxFormid(@RequestBody MessageWxFormidRequest request) {
        messageWxService.saveFormId(request.getUserId(), request.getType(), request.getFormId(), request.getCompanyKey());
        return ResultModel.success();
    }
}
